import { transports, format } from 'winston'
import dayjs from 'dayjs'
import 'winston-daily-rotate-file'

const logFormat = format.printf(({ level, message, timestamp, ...meta }) => {
  return `[Nest] ${dayjs(timestamp).format(
    'YYYY-MM-DD HH:mm:ss:SSS'
  )} [${level.toUpperCase()}] [${meta.context}] ${message}`
})

export const loggerOptions = {
  transports: [
    new transports.Console({
      format: format.combine(format.timestamp(), logFormat)
    }),
    new transports.DailyRotateFile({
      filename: 'logs/%DATE%.log',
      datePattern: 'YYYY-MM-DD-HH',
      maxSize: '20m',
      maxFiles: '14d',
      format: format.combine(format.timestamp(), logFormat)
    })
  ]
}
